﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using WcfService1.DTO;

namespace BTB
{
    public partial class FrmAdd : Page
    {
        private List<TextBox> txtNoLog;
        private List<TextBox> txtNoBundle;
        private List<TextBox> txtItemName;
        //private SearchItem si;
        private int i = 0;
        private TextBox txtCurrentItem;
        private BTBDetailDTO btbdetail;
        public BTBWrapViewModel WrapViewModel
        {
            get
            {
                return this.Resources["POW"] as BTBWrapViewModel;
            }
        }

        public BTBViewModel ViewModel
        {
            get
            {
                return this.DataContext as BTBViewModel;
            }
        }

        public FrmAdd()
        {
            InitializeComponent();
            txtNoLog = new List<TextBox>();
            txtNoBundle = new List<TextBox>();
            txtItemName = new List<TextBox>();
            
            this.Loaded += (x, y) =>
                               {
                                   (this.Resources["POW"] as BTBWrapViewModel).BTBViewModel =
                                        (BTBViewModel)this.DataContext;
                                   txtTglBTB.Focus();
                               };
            

            this.KeyDown += (x, y) =>
            {
                if (y.Key == Key.T && Keyboard.Modifiers == ModifierKeys.Control)
                    WrapViewModel.AddBTBDetail(null);
                else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                    WrapViewModel.DeleteBTBDetail(null);
                else if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.SaveBTB(ViewModel.BTB);
                else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.Cancel(null);
            };
        }

    

        //private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        //{
        //    if (e.Key == Key.Enter)
        //    {
        //        txtCurrentItem = sender as TextBox;
        //        btbdetail = txtCurrentItem.DataContext as BTBDetailDTO;
        //        e.Handled = true;
        //        ViewModel.GetItemByName(((TextBox)sender).DataContext);
        //        si = new SearchItem();
        //        si.Closed += new EventHandler(si_Closed);
        //        si.DataContext = ViewModel;
        //        si.Show();
        //    }
        //}

        //void si_Closed(object sender, EventArgs e)
        //{
        //    if (si.dg1.SelectedItem == null) return;
        //    var satuanUnit = (si.dg1.SelectedItem as ItemDTO).Satuan;
        //    btbdetail.SatuanUnit = btbdetail.SatuanUnits.FirstOrDefault(x => x.KodeSatuan == satuanUnit.KodeSatuan);
        //    btbdetail.SatuanPack =
        //        btbdetail.SatuanPacks.FirstOrDefault(x => x.KodeSatuan == (si.dg1.SelectedItem as ItemDTO).SatuanPack.KodeSatuan);
        //    DataGrid1.Focus();
        //}

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }



        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void textBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //BTBDetailDTO btbDetail = (sender as TextBox).DataContext as BTBDetailDTO;
            //if (btbDetail != null)
            //    if (btbDetail.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", btbDetail.QtyUnit);
            //    }else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", btbDetail.QtyUnit);
            //    }
        }

        private void TextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //BTBDetailDTO btbDetail = (sender as TextBox).DataContext as BTBDetailDTO;
            //if (btbDetail != null)
            //    if (btbDetail.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", btbDetail.QtyUnit);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", btbDetail.QtyUnit);
            //    }
        }

        private void AutoCompleteComboBox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            //btbdetail = (BTBDetailDTO)((AutoCompleteBox) sender).DataContext;
            ////btbdetail.SatuanUnit = btbdetail.SatuanUnits.FirstOrDefault(x => x.KodeSatuan == btbdetail.Item.Satuan.KodeSatuan);
            //btbdetail.SatuanPack =
            //    btbdetail.SatuanPacks.FirstOrDefault(x => x.KodeSatuan == btbdetail.Item.SatuanPack.KodeSatuan);

        }

      

      
       

    }
}

